<?php


namespace app\common\service;

use app\common\exception\ParameterFailedException;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style\Language;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Style\ListItem;

class Office
{
    public static function tiHuoDan($seaWaybill = [])
    {
        if (count($seaWaybill) > 8) {
            throw new ParameterFailedException(['msg' => '选中条目最大不能超过8个']);
        }
        $phpWord      = new PhpWord();
        $languageZHCN = new Language(Language::ZH_CN);
        $phpWord->getSettings()
            ->setThemeFontLang($languageZHCN);
        $phpWord->setDefaultFontSize(15);
        $phpWord->getSettings();
        $phpWord->setDefaultParagraphStyle([
            'lineHeight'  => 1.5,
            #行距
            'indentation' => [
                'left'  => 540,
                'right' => 120,
            ]
            //'spacingLineRule' => \PhpOffice\PhpWord\SimpleType\LineSpacingRule::AUTO
        ]);
        /* 注意：您附加到文档的任何元素都必须位于Section中. */

        // 在文档中添加空白部分...
        $section = $phpWord->addSection();

        /*
        * 注意：可以通过三种方式自定义添加的Text元素的字体样式：
        * - 排队;
        * -使用命名的字体样式（将隐式创建新的字体样式对象）；
        * -使用显式创建的字体样式对象。
        */

        // 正在添加具有字体自定义内联的Text元素...
        $fontStyleName = '宋体';
        $section->addText('货  物  提  货  单', [
            'name' => $fontStyleName,
            'size' => 20,
            'bold' => true,
        ], [
            'alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER,
        ]);
        //'alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER,居中
        $section->addText('致：上海泛亚航运有限公司');
        $section->addText('    华南中远海运集装箱运输有限公司湛江分公司');
        $textrun = $section->addTextRun();
        $textrun->addText('    现我司委托');
        $textrun->addText('   丁珍   ', [
            'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE,
        ]);
        $textrun->addText('先生（女士），身份证号码为：');
        $textrun->addText('  372930197201012767  ', [
            'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE,
        ]);
        $textrun->addText('到贵司办理提货手续，请贵司给予办理为盼。有关货物资料如下：');
        $textrun = $section->addTextRun();
        $textrun->addText('船名：');
        $textrun->addText('___天顺河___', [
            'underline' => Font::UNDERLINE_SINGLE,
        ]);
        $textrun->addText('航次：');

        $textrun->addText('___062S___', [
            'underline' => Font::UNDERLINE_SINGLE,
        ]);
        $textrun->addText('  ', [
            'underline' => Font::UNDERLINE_SINGLE,
        ]);

        // Define styles
        $fontStyleName = 'myOwnStyle';
        $phpWord->addFontStyle($fontStyleName, ['underline' => Font::UNDERLINE_SINGLE]);
        //
        $paragraphStyleName = 'P-Style';
        $phpWord->addParagraphStyle($paragraphStyleName, []);

        $multilevelNumberingStyleName = 'multilevel';
        $phpWord->addNumberingStyle($multilevelNumberingStyleName, [
            'type'   => 'multilevel',
            'levels' => [
                //                        text 这里可以更改序号后面的符号
                [
                    'format' => 'decimal',
                    'text'   => '%1、	B/L NO：   ',
                    'suffix' => 'nothing',
                    'start'  => 1,
                ],
            ],
        ]);

        // New section
        for ($i = 0; $i < 8; $i++) {
            if (isset($seaWaybill[$i])) {
                $section->addListItem("____{$seaWaybill[$i]['sea_waybill_number']} {$seaWaybill[$i]['count']}*{$seaWaybill[$i]['box']}____", 0, $fontStyleName, $multilevelNumberingStyleName, $paragraphStyleName);
            }
            else {
                $section->addListItem('_____________________________', 0, $fontStyleName, $multilevelNumberingStyleName, $paragraphStyleName);
            }
        }

        $section->addTextBreak(1);


        $section->addText('    以上箱子出场后产生的一切责任由我司负责，后附上有关以下单证。');
        $section->addText('    说明：在办理提货手续时必须随附上：水运提单（盖章）、身份证、委托书、抵押金等才能给予办理提货手续，谢谢合作。');

        $textrun = $section->addTextRun();
        $textrun->addText('                                        盖公章');
        $textrun->addTextBreak();
        $textrun->addText('                                     签名：');
        $textrun->addTextBreak();
        $textrun->addText('                                    年     月     日     ');
        $objWriter = IOFactory::createWriter($phpWord, 'Word2007');
        $filename  = '中远' . $seaWaybill[0]['sea_waybill_number'] . '等-货物提货单' . '.docx';
        header('Content-Description: File Transfer');
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Expires: 0');
        $objWriter->save('php://output');
        die;
    }
}